package main

import (
	"context"
	"fmt"
	"log"

	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/consumer"
	"github.com/apache/rocketmq-client-go/v2/primitive"
)

func main() {
	c, err := rocketmq.NewPushConsumer(
		consumer.WithNameServer([]string{"127.0.0.1:9876"}),
		consumer.WithGroupName("TagGroupA"),
		consumer.WithConsumerModel(consumer.Clustering),
	)
	if err != nil {
		log.Fatalf("创建消费者失败: %s", err)
	}

	err = c.Subscribe("TagTopic", consumer.MessageSelector{
		Type:       consumer.TAG,
		Expression: "TagA",
	}, func(ctx context.Context, msgs ...*primitive.MessageExt) (consumer.ConsumeResult, error) {
		for _, msg := range msgs {
			fmt.Printf("消费到TagA消息: %s\n", string(msg.Body))
		}
		return consumer.ConsumeSuccess, nil
	})
	if err != nil {
		log.Fatalf("订阅失败: %s", err)
	}

	err = c.Start()
	if err != nil {
		log.Fatalf("启动消费者失败: %s", err)
	}

	select {}
}
